package forms;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

import net.miginfocom.swing.MigLayout;

public class OptionsForm extends JDialog {
	private static final long serialVersionUID = 6990581556803465553L;
	JLabel visualStyleLabel;
	JComboBox visualStyleCB;
	JButton okBtn, cancelBtn;
	JPanel guiSettingsPanel;
	Map<String, String> styleMap;
	/*
	 	javax.swing.plaf.metal.MetalLookAndFeel
		com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel
		com.sun.java.swing.plaf.motif.MotifLookAndFeel
		com.sun.java.swing.plaf.windows.WindowsLookAndFeel
		com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel
	 */
	public OptionsForm() {
		setLayout(new MigLayout());
		setTitle("Options...");
		guiSettingsPanel = new JPanel(new MigLayout());
		guiSettingsPanel.setBorder(BorderFactory.createTitledBorder("Gui settings"));
		styleMap = new HashMap<String, String>();
		okBtn = new JButton("OK");
		cancelBtn = new JButton("Cancel");
		visualStyleLabel = new JLabel("Look and feel:");
		visualStyleCB = new JComboBox();
		String key = "",current="";
		current = (UIManager.getLookAndFeel().getClass().getCanonicalName().substring(UIManager.getLookAndFeel().getClass().getCanonicalName().lastIndexOf(".")+1)).split("LookAndFeel")[0];
		for(LookAndFeelInfo info:UIManager.getInstalledLookAndFeels()) {
			key = (info.getClassName().substring(info.getClassName().lastIndexOf(".")+1)).split("LookAndFeel")[0];
			styleMap.put(key, info.getClassName());
			visualStyleCB.addItem(key);
		}
		visualStyleCB.setSelectedItem(current);
		guiSettingsPanel.add(visualStyleLabel);
		guiSettingsPanel.add(visualStyleCB,"grow");
		add(guiSettingsPanel,"span,wrap");
		add(okBtn,"tag ok");
		add(cancelBtn,"tag cancel");
		okBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String lf = (String)visualStyleCB.getSelectedItem();
				MainForm.getInstance().setLookAndFeel(styleMap.get(lf));
				dispose();
			}
		});
		cancelBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				dispose();				
			}
		});
	}
}
